#coding:utf-8
import signal
from twisted.internet import reactor

class Stuff:
	def __init__(self):
		self.isTrying = True
		self.activeBots = {}

Stuff = Stuff()

def activeIterBots():
	return iter(Stuff.activeBots)

def slotDisconnect(handle):
	del Stuff.activeBots[id(handle)]
	if  not Stuff.activeBots:
		reactor.stop()

def registry(handl):
	Stuff.activeBots[id(handl)] = handl
	handl.eventDisconnect = slotDisconnect

def sigint(*args):
	if Stuff.isTrying:
		Stuff.isTrying = False
		for handler in Stuff.activeBots.values():
			handler.send(u"<presence type='unavailable'><status>Выключаюсь Ctrl+C</status></presence></stream:stream>")

signal.signal(signal.SIGINT,sigint)

__all__ = ["slotDisconnect","registry","activeIterBots"]